<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use app\models\TblUser_reg_form;

class SiteController extends Controller
{

    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout'],
                'rules' => [
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    public function actionIndex()
    {
        return $this->render('index');
    }

    public function actionLogin()
    {
        if (!\Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
        
        	//echo "登陆  陈工";
        	//exit;
          return $this->goBack();
        }
        return $this->render('login', [
            'model' => $model,
        ]);
    }

    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }

    public function actionContact()
    {
        $model = new ContactForm();
        if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
            Yii::$app->session->setFlash('contactFormSubmitted');

            return $this->refresh();
        }
        return $this->render('contact', [
            'model' => $model,
        ]);
    }

    public function actionAbout()
    {
    	
    	echo "whoelcome to ..";
        //return $this->render('about');
    }
    
    
    public function actionReg()
    {
    	
    	
    	//echo "fdsfsdf";
    	//exit;
    	
    	
    	
    	
    	$model = new TblUser_reg_form();
    	
    	//$model = new app\models\TblUser_reg_form();
    	
    	//exit;
    	if ($model->load(Yii::$app->request->post())) {
    		$user = $model->signup();
    		
    		var_dump($user);
    		//exit;
    		if ($user) {
    			// $login = new SiteLoginForm();
    			
    			 //yii::$app->getUser()->
    			 //echo "jianlai";
    			// exit;
    			$c= Yii::$app->user->login($user);
    			
    			print_R($c);
    			//exit;
    			$flage = Yii::$app->getUser()->login($user);
    			print_R($flage );
    			//exit;
    			if($flage) {
    				
    				
    				echo "你已经登陆了";
    				$session = \Yii::$app->session;
    				var_dump($session);
    				
    				IF( \Yii::$app->getUser()->isGuest )
    				{
    					ECHO 1;
    				}ELSE
    				{
    					ECHO 2;
    					
    					ECHO Yii::$app->user->identity->username ;
    					
    					return  $this->goBack();
    				}
    				
    				
    				EXIT;
    				
    				//return $this->goHome();
    			}
    			else
    			{
    				var_dump($user);
    				exit;

    			}
    		}
    	}
    	
    	
    	
    
    	if ($model->load(Yii::$app->request->post())) {
    		if ($model->validate()) {
    			// form inputs are valid, do something here
    			
    		//	print_R($_SESSION);
    			echo "dfsdfsdfsd";
    			print_R($_REQUEST);
    			exit;
    			//return;
    		}
    	}
    
    	return $this->render('reg', [
    			'model' => $model,
    	]);
    }
}
